import gzip
import shutil
from multiprocessing import Pool
# exec(open(P_Mod / 'Stata.py').read())

f_GS = P_GS_Data / 'GS' / 'gas_station.h5'
P_GS_Fig = P_GS / 'Figures'

dict_strYMD_to_dtYM = load_obj(P_Research / 'Others' / 'DateTime' / 'dict_strYMD_to_dtYM.pkl')

h_min = 7; h_max = 21
ls_fuel = ['e5','e10','diesel']

P_MAX = 3000; P_MIN = 500
DATE_RECORD_1MIN = Timestamp('2016-03-01')
try:
	dict_brandid_to_brand = load_obj(P_GS_Data / 'GS' / 'dict_brandid_to_brand.pkl')
	dict_stid_to_brandid = load_obj(P_GS_Data / 'GS' / 'dict_stid_to_brandid.pkl')
	dict_sizegp_to_names = load_obj(P_GS_Data / 'GS' / 'dict_sizegp_to_names.pkl')
	nSizeGp = len(dict_sizegp_to_names)
except:
	pass

def clean_gas_station(df):
    df['ID'] = df['ID'].astype(str)
    df['Name'] = df['Name'].astype(str)
    df['Brand'] = df['Brand'].astype(str)
    df['St'] = df['St'].astype(str)
    df['StNum'] = df['StNum'].astype(str)
    df['Post'] = to_numeric(df.Post, errors='coerce').fillna(0).astype(int)
    df['City'] = df['City'].astype(str)
    df['Lat'] = df['Lat'].astype(float)
    df['Lng'] = df['Lng'].astype(float)
    return df

def clean_price_history(df):
    del df['index']
    df['ID'] = df['ID'].astype(str)
    df['e5'] = df['e5'].astype(int)
    df['e10'] = df['e10'].astype(int)
    df['diesel'] = df['diesel'].astype(int)
    df['Time'] = to_datetime(df.Time.astype(str).str[:-6])
    df['Changed'] = df['Changed'].astype(int)
    return df


def separate_me_rival_pair(s):
    ls_key = s.unique().tolist()
    ls_value = [pair.split('-') for pair in ls_key]
    arr_value = np.array(itemgetter(*s.tolist())({k:v for k,v in zip(ls_key, ls_value)})).astype(int)
    return arr_value[:,0], arr_value[:,1]

def swap_elts_around_dash(df, col):
    df = df.copy()
    s = df[col].str.split('-')
    df[col] = s.str[1] + '-' + s.str[0]
    return df


